/**
 * Created by lenovo on 2019/11/30.
 */

var prefix = ctx + "self/testinfo";
var m;
$(function() {
    var innerHtml = "";
    var formData = new FormData();
    var j = 1;
    var choose =["A","B","C","D","E","F"];
    var type;
    var chboxtype;
    $.ajax({
        url: prefix + "/list2",
        type: 'post',
        cache: false,
        data: formData,
        processData: false,
        contentType: false,
        dataType: "json",
        success: function(result) {
            //console.log(result);
            $.each(result,function(i,n) {
                if(n.type==1) {
                    type = "单选题";
                    chboxtype="radio";
                } else if (n.type==2){
                    type = "多选题";
                    chboxtype="checkbox";
                } else if (n.type==3) {
                    type = "填空题";
                    chboxtype="text";
                }
                var strs= new Array(); //定义一数组
                strs=n.chooses.split("|"); //字符分割
                innerHtml += "<div class='row'><div class='col-sm-2'></div><div class='col-sm-8' ><ul class='list-unstyled'>";
                innerHtml += ("<li><input type='hidden' name='testnumber' value='" + n.id + "' /></li>");
                if (n.type!=3) {
                    innerHtml += ("<li>" + j + "、" + n.content + "(" + type + ")</li>");
                    for (i = 0; i < strs.length; i++) {
                        innerHtml += ("<li><input type='" + chboxtype + "'  name='" + n.id + "' value='" + choose[i] + "' /> " + strs[i] + " </li><li>");
                    }
                } else {
                    contentLsit=n.content.split("$"); //字符分割
                    innerHtml += ("<li>" + j + "、" + contentLsit[0] + "<input type='" + chboxtype + "' name='testanswer' style='border:1;border-bottom:1 solid black;background:;'>" + contentLsit[1]+ "(" + type + ")</li>");
                }
                innerHtml += "</ul></div><div class='col-sm-2'></div></div><br>";
                j++;
            });
            $("#testlist").html(innerHtml);
        }
    });
});

$(function () {
    //若修改时间，还需修改提交提交时间函数中计算用时
    m = 10;
    var s = 10;
    setInterval(function () {
        if (m >= 0) {
            if (s < 10) {
                $('#time').html("剩余时间：" + m + ':0' + s);
            } else {
                $('#time').html("剩余时间：" + m + ':' + s);
            }
            s--;
            if (s < 0) {
                s = 59;
                m--;
            }
            if (m == 0 && s < 1) {
                copyText("over");
            }
        }
    }, 1000)
});

function copyText(str) {
    console.log("start");
    var packageCodeList=new Array();
    $('ul').each(function(i,n){
        var va = "";
        var number = $(this).find('input[type="radio"]:checked').length;


        var testNumber = $(this).find('input[name="testnumber"]').val();

        if (number != 0) {
            va = $(this).find('input[type="radio"]:checked').val();
              //console.log("va ：" ,va);
        } else {
            $(this).find('input[type="checkbox"]:checked').each(function(){
                va += $(this).val();
            });
            //获取填空题答案
            if (va == "") {
                va += $(this).find('input[type="text"]').val();
                va = va.trim();
            }
        }
        console.log("va ：" ,va);

        packageCodeList.push({
            testNumber:testNumber,   //名称修改为对应实体类的字典
            value:va
        });
    });

    var checkFlag = 1;
    for (i=0;i<packageCodeList.length ;i++ ) {
        if(packageCodeList[i]["value"] == "" || packageCodeList[i]["value"] == null || packageCodeList[i]["value"] == "undefined") {
            checkFlag = 0
        }
    }

    console.log("packageCodeList ：" ,packageCodeList);
    console.log("packageCodeList.length ：" ,Object.keys(packageCodeList).length);  //获取字典对象长度
    console.log("packageCodeList.JSON ：" ,JSON.stringify(packageCodeList));
    var n=10-m;
    console.log("use time is ：" ,n);

    var test = "test";
    var tempParam = new Array();
    tempParam.push({
        test:test,
        useTime:n,
        anaSwer:packageCodeList
    });
    var data = JSON.stringify(tempParam);
    var testId = $("#testId").val();
    var testType = $("#testType").val();
    console.log("testId:",testId);
    console.log("testType:",testType);
    var formData = new FormData();
    formData.append('useTime', n);
    formData.append('testId', testId);
    formData.append('testType', testType);
    formData.append('anaSwer', JSON.stringify(packageCodeList));


    if (checkFlag == 0 &&  str != "over") {
        $.modal.confirm("还要考题尚未作答，是否提交？", function () {
            touchTest(formData);
        });
    } else if (str != "over"){
        $.modal.confirm("确定提交？", function () {
            touchTest(formData);
        });
    } else {
        touchTest(formData);
    }

}


function touchTest(formData) {
    $.ajax({
        url: prefix + "/checktest",
        type: 'post',
        cache: false,
        data:formData,
        processData: false,
        contentType:false,
        dataType: "json",
        success: function(result) {
            if (result.code == web_status.SUCCESS) {
                $.modal.alertSuccess(result.msg);
                top.document.location.href='/';
            } else if (result.code == web_status.WARNING) {
                $.modal.alertWarning(result.msg)
            } else {
                $.modal.alertError(result.msg);
            }
        }
    });
}

